package com.blast.comicsisland.adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.blast.comicsisland.R;
import com.blast.comicsisland.model.recommend.recommendbody.ComicsList;
import com.blast.comicsisland.utils.ImageLoaderUtil;

import java.util.List;

/**
 * Created by Android Studio
 * Author: hg
 * Date: 15/6/29
 * Email: 531057892@qq.com
 */
public class RankItemAdapter extends BaseAdapter {

    private List<ComicsList> comicsLists;
    private Context context;
    private final ImageLoaderUtil imageLoaderUtil;
    private final ImageLoader imageLoader;

    public RankItemAdapter(Context context, List<ComicsList> comicsLists) {
        this.context = context;
        this.comicsLists = comicsLists;
        imageLoaderUtil = ImageLoaderUtil.getInstance();
        imageLoader = imageLoaderUtil.getImageLoader();
    }

    @Override
    public int getCount() {
        int ret = 0;
        if (comicsLists != null) {
            ret = comicsLists.size();
        }
        return ret;
    }

    @Override
    public Object getItem(int position) {
        Object ret = null;
        if (comicsLists != null) {
            ret = comicsLists.get(position);
        }
        return ret;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View ret = null;
        if (convertView != null) {
            ret = convertView;
        } else {
            ret = LayoutInflater.from(context).inflate(R.layout.item_rank, parent,false);
        }

        ViewHolder holder = (ViewHolder) ret.getTag();
        if (holder == null) {
            holder = new ViewHolder();
            holder.imageView = (NetworkImageView) ret.findViewById(R.id.item_rank_item_image);
            holder.txtTitle = (TextView) ret.findViewById(R.id.item_rank_item_title);
            holder.txtAuthor = (TextView) ret.findViewById(R.id.item_rank_item_author);
            holder.txtViewNum = (TextView) ret.findViewById(R.id.item_rank_item_renqi);
            ret.setTag(holder);
        }
        ComicsList comicsList = comicsLists.get(position);
        if (comicsList != null) {
            String bigbookName = comicsList.getBigbookName();
            String bigbookAuthor = comicsList.getBigbookAuthor();
            String bigbookview = comicsList.getBigbookview();
            String coverurl = comicsList.getCoverurl();
            bigbookAuthor = bigbookAuthor.replaceAll("@"," ");
            holder.imageView.setImageUrl(coverurl,imageLoader);
            holder.txtTitle.setText(bigbookName);
            holder.txtAuthor.setText(bigbookAuthor);
            holder.txtViewNum.setText(bigbookview);
        }


        return ret;
    }

    class ViewHolder {
        private NetworkImageView imageView;
        private TextView txtTitle;
        private TextView txtAuthor;
        private TextView txtViewNum;

    }

}
